<template>
  <div style="height: 100%;width: 100%;overflow: auto;">
    <div class="login-container">
      <div class="word-container">
        <div class="entitle-container">
          云上数值计算新体验
        </div>
        <div class="segment-container">
          结合云计算能力和丰富的函数库，用户能更简单、更快速的编写和运行数值计算
        </div>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="loginForm.email"
            placeholder="邮箱"
            name="email"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>

        <el-tooltip
          v-model="capsTooltip"
          content="大写锁定已打开"
          placement="right"
          :tabindex="-1"
          manual
        >
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span
              class="show-pwd"
              @click="showPwd"
            >
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
        <el-link
          :underline="false"
          class="login-form-register"
          type="primary"
          @click="$router.push('/register')"
        >
          注册
        </el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  userLogin
} from '@/api/userLogin';
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        email: '',
        password: ''
      },
      loginRules: {
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {}
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        // 用来处理从其他页面重定向到login
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.email === '') {
      this.$refs.email.focus();
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus();
    }
  },
  methods: {
    checkCapslock(e) {
      this.capsTooltip = e.getModifierState('CapsLock');
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$store.commit('matlab/clearEditorFiles');
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          userLogin(this.loginForm).then((data) => {
            this.loading = false;
            if (data.result === 0) {
              this.$message.success('登录成功');
              const prefix = (data.data.uid === 23) ? '' : data.data['userPrefix'] + '_';
              this.$store.commit('user/SET_DATA', {
                email: this.loginForm.email,
                uid: data.data.uid,
                token: {
                  token: data.data.token,
                  lastTime: new Date().getTime()
                },
                username: data.data.username,
                userprefix: prefix
              });
              this.$store.commit('matlab/setUid', data.data.uid);
              if (this.redirect === undefined) {
                this.$router.push('/');
              } else {
                this.$router.push(this.redirect);
              }
            } else {
              this.$message.warning(data.rmsg);
            }
          });
        } else {
          console.error('error submit!!!');
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $light_gray;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 30px;
    width: 85%;
    input {
      background: transparent;
      border: 2px blue;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #454545;
      height: 30px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white inset;
        -webkit-text-fill-color: #454545 !important;
      }
      &:autofill {
        box-shadow: 0 0 0px 1000px white inset;
        -webkit-text-fill-color: #454545 !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(236, 208, 208, 0.1);
    background: white;
    color: #454545;
    margin-bottom: 35px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100%;
  width: 100%;
  min-height: 720px;
  min-width: 1280px;
  background-image: url('..//assets/images/background.png');
  background-size: 100% 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  .word-container {
    position: relative;
    margin: 20px;
    background: transparent;
    width: 700px;
    height: 300px;
    color: white;
    .entitle-container {
      margin-left: 10px;
      font-size: 60px;
    }
    .segment-container {
      margin-top: 10px;
      font-size: 20px;
    }
  }
  .login-form {
    height: 310px;
    width: 390px;
    max-width: 100%;
    padding: 45px 35px 20px 35px;
    background-color: rgba(255, 255, 255, 0.8);
    position: relative;
    .login-form-register {
      position: absolute;
      right: 35px;
      bottom: 20px;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .show-pwd {
    position: absolute;
    right: 12px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-button + .el-button {
    margin-left: 0px;
  }
  .thirdparty-button {
    position: relative;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
